package com.guat.servlet.article;

import com.google.gson.Gson;
import com.guat.entity.Article;
import com.guat.entity.User;
import com.guat.service.ArticleService;
import com.guat.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/article")
public class ArticleServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ArticleService articleService = new ArticleService();
        List<Article> list = articleService.getAll();
        //设置返回前端数据使用UTF-8编码,防止乱码
        resp.setCharacterEncoding("UTF-8");
        //设置响应内容为json,以便前端正确解析返回的数据
        resp.setContentType("application/json");
        //将Map集合转换为json
        Gson gson = new Gson();
        String mapJson = gson.toJson(list);
        //将数据写入输入流中
        resp.getWriter().write(mapJson);
    }
}
